﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using Bzure.Security.Principal;
using Bzure.Common.Patterns;
using Bzure.Security;
using Bzure.Common.Core;

namespace Bzure.Apps.Security.DataAccess
{
    public class AccountProvider : IAccountContract, IUserContract, IUserGroupContract
    {
        #region IAccountContract
        /// <summary>
        /// 事务上下文
        /// </summary>
        public Bzure.Common.Transactions.ITransactionContext TransactionContext { get; set; }
        public string TransactionName { get; set; }
        /// <summary>
        /// 创建而不打开连接
        /// </summary>
        /// <returns></returns>
        public System.Data.Common.DbConnection CreateConnection()
        {
            return ConnectionFactory.GetConnection();
        }
        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="password">密码明文</param>
        /// <returns>密码密文</returns>
        public string EncryptPassword(string password)
        {
            Encoding _encoding = Encoding.Default;
            byte[] bb = _encoding.GetBytes(password);

            byte[] result;

            SHA1 sha = new SHA1CryptoServiceProvider();
            result = sha.ComputeHash(bb);
            return BitConverter.ToString(result);

            //return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
        }

        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="Password">密码密文</param>
        /// <returns></returns>
        public bool ValidateUser(string UserName, string Password)
        {
            return new UserRepository(this).ValidateUser(UserName, Password);
        }

        /// <summary>
        /// 创建新用户
        /// </summary>
        /// <param name="User"></param>
        /// <returns>是否成功</returns>
        public CreateUserResult CreateUser(BzureUser User)
        {
            if (ExistsUserID(User.UserID))
                return CreateUserResult.UserIDExists;
            if (ExistsUserName(User.UserName))
                return CreateUserResult.UserNameExists;
            //注册用户可以没有邮箱,但如果有就不能重复
            if (User.EMail.IsNotNullAndEmpty() && ExistsEMail(User.EMail))
                return CreateUserResult.EMailExists;
            return new UserRepository(this).CreateUser(User) ? CreateUserResult.Success : CreateUserResult.OtherFailed;
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="EMail"></param>
        /// <returns></returns>
        public BzureUser GetUserByEMail(string EMail)
        {
            if (EMail.IsNullOrEmpty())
                return default(BzureUser);
            return new UserRepository(this).GetUserByEMail(EMail);
        }
        /// <summary>
        /// 改变用户密码
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="Password">密码密文</param>
        /// <returns>是否成功</returns>
        public bool SetPassword(string UserName, string Password)
        {
            return new UserRepository(this).ChangePassword(UserName, Password);
        }
        /// <summary>
        /// 设置用户状态
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public bool SetUserState(string UserName, int State)
        {
            return new UserRepository(this).SetUserState(UserName, State);
        }
        /// <summary>
        /// 用户是否存在
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public bool ExistsUserName(string UserName)
        {
            return new UserRepository(this).ExistsUserName(UserName);
        }
        /// <summary>
        /// 用户是否存在
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public bool ExistsUserID(string UserID)
        {
            return new UserRepository(this).ExistsUserID(UserID);
        }
        /// <summary>
        /// 获取用户实体
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public BzureUser GetUserByName(string UserName)
        {
            return new UserRepository(this).GetUserByName(UserName);
        }
        /// <summary>
        /// 获取用户的密码找回令牌
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public PasswordToken GetUserPasswordToken(string UserName)
        {
            return new PasswordTokenRepository(this).GetUserPasswordToken(UserName);
        }
        /// <summary>
        /// 获取用户的密码找回令牌
        /// </summary>
        /// <param name="Token">令牌</param>
        /// <returns></returns>
        public PasswordToken GetPasswordToken(string Token)
        {
            return new PasswordTokenRepository(this).GetPasswordToken(Token);
        }
        /// <summary>
        /// 获取用户实体
        /// </summary>
        /// <param name="OpenID">OpenID</param>
        /// <returns></returns>
        public BzureUser GetUserByOpenID(string OpenID)
        {
            return new UserRepository(this).GetUserByOpenID(OpenID);
        }
        #endregion
        #region IUserContract
        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="EMail"></param>
        /// <param name="EncryptPassword"></param>
        /// <returns></returns>
        public bool ValidateEMailUser(string EMail, string EncryptPassword)
        {
            if (EMail.IsNullOrEmpty())
                return false;
            return new UserRepository(this).ValidateEMailUser(EMail, EncryptPassword);
        }
        /// <summary>
        /// 邮箱是否存在
        /// </summary>
        /// <param name="NickName"> 邮箱</param>
        /// <returns></returns>
        public bool ExistsEMail(string EMail)
        {
            return new UserRepository(this).ExistsEMail(EMail);
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public BzureUser[] GetAllUsers()
        {
            return new UserRepository(this).GetAllUsers();
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userid"></param>
        public bool DeleteUserByID(string userid)
        {
            return new UserRepository(this).DeleteUser(userid);
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public BzureUser GetUserByID(string userid)
        {
            return new UserRepository(this).GetUserByID(userid);
        }
        /// <summary>
        /// 根据绑定获得用户
        /// </summary>
        /// <param name="BindID"></param>
        /// <param name="BindProvider"></param>
        /// <returns></returns>
        public BzureUser GetUserByBind(string BindID, string BindProvider)
        {
            return new UserBindRepository(this).GetUserByBind(BindID, BindProvider);
        }
        public BzureUser[] GetUsers(string[] Ids)
        {
            return new UserRepository(this).GetUsers(Ids);
        }
        /// <summary>
        /// 创建用户ID
        /// </summary>
        /// <returns></returns>
        public string GenerateUserID()
        {
            return Bzure.Common.Configuration.AppConfiguration.IDGenerator.GenerateID("BZUREUSERIDKEY", 99, "U", 7);
        }
        public string GenerateRoleID()
        {
            return Bzure.Common.Configuration.AppConfiguration.IDGenerator.GenerateID("BZUREROLEIDKEY", 9, "R", 2);
        }
        /// <summary>
        /// 昵称是否存在
        /// </summary>
        /// <param name="NickName"> 昵称</param>
        /// <returns></returns>
        public bool ExistsNickName(string NickName)
        {
            return new UserRepository(this).ExistsNickName(NickName);
        }
        /// <summary>
        /// 设置昵称
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="NickName">昵称</param>
        /// <returns></returns>
        public bool SetNickName(String UserName, string NickName)
        {
            return new UserRepository(this).SetNickName(UserName, NickName);
        }
        /// <summary>
        /// 获取总用户数
        /// </summary>
        /// <returns></returns>
        public long GetAllUserCount()
        {
            return new UserRepository(this).GetAllUserCount();
        }
        #endregion
        #region IUserGroupContract
        public UserGroup[] GetUserGroups(string[] GroupIDs)
        {
            return new UserGroupRepository(this).GetUserGroups(GroupIDs);
        }
        #endregion
    }
}
